package com.yhos.platform.oopston.core.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.*;
import java.util.Enumeration;
import java.util.regex.Pattern;

/**
 * WebUtil
 * 
 * @author Feng yan
 * @version 1.0
 */
@SuppressWarnings("all")
public class WebUtil {

	private static final Pattern IP_PATTERN = Pattern.compile("\\d{1,3}(\\.\\d{1,3}){3,5}$");

	private static final Pattern LOCAL_IP_PATTERN = Pattern.compile("127(\\.\\d{1,3}){3}$");

	public static final String ANYHOST = "0.0.0.0";

	public static final String LOCALHOST = "127.0.0.1";

	private WebUtil() {
	}

	public static String getHostAddress() {
		InetAddress address = null;
		try {
			address = InetAddress.getLocalHost();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}// 获取的是本地的IP地址 //PC-20140317PXKX/192.168.0.121
		String hostAddress = address.getHostAddress();// 192.168.0.121

		if (hostAddress.equals("127.0.0.1") || hostAddress.equals("0.0.0.0")) {
			hostAddress = getNewHostAddress().getHostName();
			if (hostAddress.equals("172.19.250.54")) {
				hostAddress = "http://" + "47.100.244.65";
			}
		} else {
			if (hostAddress.equals("172.19.250.54")) {
				hostAddress = "http://" + "47.100.244.65";
			} else {
				hostAddress = "http://" + hostAddress;
			}
		}
		return hostAddress;
	}

	public static String getHostUrl(String url) {
		String hostUrl = "";
		try {
			URL u = new URL(url);
			hostUrl = u.getProtocol() + "://" + u.getHost();
		} catch (MalformedURLException e) {
			throw new IllegalArgumentException(e.getMessage(), e);
		}
		return hostUrl;
	}

	public static String getPath(String url) {
		String getPath = "";
		try {
			URL u = new URL(url);
			getPath = u.getPath();
		} catch (MalformedURLException e) {
			throw new IllegalArgumentException(e.getMessage(), e);
		}
		return getPath.substring(1, getPath.length());
	}

	public static String getFile(String url) {
		String getFile = "";
		try {
			URL u = new URL(url);
			getFile = u.getFile();
		} catch (MalformedURLException e) {
			throw new IllegalArgumentException(e.getMessage(), e);
		}
		return getFile;
	}

	public static String getContextPath(HttpServletRequest request) {
		return getHostUrl(request.getRequestURL().toString()).replace("https", "http") + request.getContextPath();
	}

	public static HttpServletRequest getRequest() {
		return ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
	}

	public static HttpSession getSession() {
		return getRequest().getSession();
	}

	@SuppressWarnings("unchecked")
	public static <T> T getSessionAttr(String attrName) {
		return (T) getSession().getAttribute(attrName);
	}

	public static ServletContext getServletContext() {
		return getSession().getServletContext();
	}

	public static String getWebRoot(HttpServletRequest request) {
		String path = request.getSession().getServletContext().getRealPath("/");
		path = path.replace("\\", "/");
		return path;
	}

	public static InetAddress getNewHostAddress() {
		InetAddress localAddress = null;
		Enumeration<NetworkInterface> interfaces;
		try {
			interfaces = NetworkInterface.getNetworkInterfaces();
		} catch (SocketException e) {
			throw new IllegalStateException(e.getMessage(), e);
		}
		if (interfaces != null) {
			while (interfaces.hasMoreElements()) {
				NetworkInterface network = interfaces.nextElement();
				Enumeration<InetAddress> addresses = network.getInetAddresses();
				if (addresses != null) {
					while (addresses.hasMoreElements()) {
						InetAddress address = addresses.nextElement();
						if (isValidAddress(address)) {
							return address;
						}
					}
				}
			}
		}
		return localAddress;
	}

	private static boolean isValidAddress(InetAddress address) {
		if (address == null || address.isLoopbackAddress())
			return false;
		String name = address.getHostAddress();
		return (name != null && !ANYHOST.equals(name) && !LOCALHOST.equals(name) && IP_PATTERN.matcher(name).matches());
	}

}
